package cn.qingyun.gis.modules.enterprise.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.NumberUtil;
import cn.qingyun.gis.modules.enterprise.entity.Point;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

@Component
@Slf4j
public class LocationTool {
    private static final ExecutorService executorService = Executors.newFixedThreadPool(8);
    private static String gdServiceHost;

    public static Point parsePoint(String pointStr) {
        return parsePoint(pointStr, " ");
    }

    public static Point parsePoint(String pointStr, String separator) {
        try {
            String location = pointStr.replaceAll("POINT\\(", "").replaceAll("\\)", "");

            String[] split = location.split(separator);
            Point point = new Point();
            point.setLon(NumberUtil.toBigDecimal(split[0]).setScale(6, RoundingMode.UP));
            point.setLat(NumberUtil.toBigDecimal(split[1]).setScale(6, RoundingMode.UP));

            return point;
        } catch (Exception e) {
            return new Point(BigDecimal.ZERO, BigDecimal.ZERO);
        }
    }


    //MULTILINESTRING((121.39943495 37.53428439,121.39942437 37.53418918,121.39940483 37.53401123,121.39938504 37.53385415,121.39938504 37.53385415,121.39935953 37.53365152,121.39932834 37.53347574,121.39932834 37.53347574,121.39929389 37.53328097,121.3992669 37.53315146,121.3992669 37.53315146,121.39922471 37.53294894,121.39922471 37.53294894,121.39905029 37.531789,121.3989898 37.53146267,121.39895941 37.53114556,121.39897036 37.53094317,121.39897036 37.53094317,121.39898084 37.53074951,121.39901333 37.53046755,121.39903112 37.53031318,121.39903112 37.53031318,121.39905083 37.53016357,121.39904955 37.53005918,121.39904955 37.53005918,121.39904506 37.52969308,121.39904506 37.52969308,121.39904092 37.52935436,121.39904092 37.52935436,121.39902965 37.52889145,121.39902965 37.52889145,121.39903591 37.52881465),(121.39902965 37.52889145,121.39903591 37.52881465),(121.39943495 37.53428439,121.39942437 37.53418918,121.39940483 37.53401123,121.39938504 37.53385415))

    public static List<Point> parseWkt(String wkt) {
        List<Point> points = Lists.newArrayList();
        try {
            if (CharSequenceUtil.isNotBlank(wkt)) {
                String str = wkt.replaceAll("MULTIPOLYGON\\(\\(\\(", "").replaceAll("\\)\\)\\)", "");
                for (String location : str.split(",")) {
                    String[] split = location.split(" ");
                    Point point = new Point();
                    point.setLon(NumberUtil.toBigDecimal(split[0]));
                    point.setLat(NumberUtil.toBigDecimal(split[1]));

                    points.add(point);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return points;
    }

    public static List<List<Point>> parseMultiWkt(String wkt) {
        List<List<Point>> result = Lists.newArrayList();
        try {
            if (CharSequenceUtil.isNotBlank(wkt)) {
                String[] strings = wkt.split("\\),\\(");

                for (String subAoi : strings) {
                    List<Point> points = Lists.newArrayList();
                    String str = subAoi.replaceAll("\\(", "").replaceAll("\\)", "").replaceAll("MULTILINESTRING", "");
                    for (String location : str.split(",")) {
                        String[] split = location.split(" ");
                        Point point = new Point();
                        point.setLon(NumberUtil.toBigDecimal(split[0]));
                        point.setLat(NumberUtil.toBigDecimal(split[1]));

                        points.add(point);
                    }

                    result.add(points);
                }
            }
        } catch (Exception e) {
            log.error("地址转换错误", e);
        }
        return result;
    }

    public static String formatWkt(List<Point> wkts) {
        if (CollUtil.isNotEmpty(wkts)) {
            return String.format("MULTIPOLYGON(((%s)))",
                    wkts.stream().map(item -> item.getLon() + " " + item.getLat()).collect(Collectors.joining(","))
            );
        } else {
            return "";
        }
    }

    public static String formatMultiWkt(List<List<Point>> wkts) {
        if (CollUtil.isNotEmpty(wkts)) {
            List<String> strWkts = Lists.newArrayList();
            for (List<Point> wkt : wkts) {
                strWkts.add(wkt.stream().map(item -> item.getLon() + " " + item.getLat()).collect(Collectors.joining(",")));
            }

            return String.format("MULTILINESTRING((%s))", String.join("),(", strWkts));
        } else {
            return "";
        }
    }

    public static String formatPoint(BigDecimal lon, BigDecimal lat) {
        return String.format("%s,%s", lon, lat);
    }


}
